package com.shundr.auth.common.view;

import java.util.Calendar;
import java.util.Date;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import com.shundr.auth.channel.BuildConfig;
import com.shundr.auth.channel.R;
import com.shundr.auth.activity.AuthCompanyActivity;
import com.shundr.auth.activity.AuthDriverActivity;
import com.shundr.auth.activity.AuthOwnnerActivity;
import com.shundr.auth.common.util.DateUtils;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.model.UserInfo;

/**
 * 
 * DialogAuth
 * 
 * @author larry 2015-4-16 下午7:04:38
 * @version 1.0.0
 *
 */
public class DialogChoiceBeginEndTime extends Dialog implements
		android.view.View.OnClickListener, OnDateChangedListener {
	private Context mContext;
	private Date begin;
	private Date end;
	private DatePicker dpBegin;
	private DatePicker dpEnd;
	private Button btnOK;
	private Button btnCancel;

	private DateChoiseListener listener;

	private Calendar canlendar = Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// View view = inflater.inflate(R.layout.dialog_go_auth, null);
		setContentView(R.layout.dialog_select_begin_end_time);

		dpBegin = (DatePicker) findViewById(R.id.dp_begin);
		dpEnd = (DatePicker) findViewById(R.id.dp_end);
		hideCalender();

		canlendar.setTime(begin);
		dpBegin.init(canlendar.get(Calendar.YEAR),
				canlendar.get(Calendar.MONTH),
				canlendar.get(Calendar.DAY_OF_MONTH), this);
		canlendar.setTime(end);
		dpEnd.init(canlendar.get(Calendar.YEAR), canlendar.get(Calendar.MONTH),
				canlendar.get(Calendar.DAY_OF_MONTH), this);

		btnOK = (Button) findViewById(R.id.btn_ok);
		btnCancel = (Button) findViewById(R.id.btn_cancel);
		btnOK.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		Window dialogWindow = this.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);

		/*
		 * lp.x与lp.y表示相对于原始位置的偏移.
		 * 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
		 * 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
		 * 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
		 * 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
		 * 当参数值包含Gravity.CENTER_HORIZONTAL时
		 * ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
		 * 当参数值包含Gravity.CENTER_VERTICAL时
		 * ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
		 * gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
		 * Gravity.CENTER_VERTICAL.
		 * 
		 * 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
		 * 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了, Gravity.LEFT, Gravity.TOP,
		 * Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
		 */
		lp.width = (int) (LContext.width * 0.8); // 宽度
		lp.height = LayoutParams.WRAP_CONTENT; // 高度
		dialogWindow.setAttributes(lp);

		setCanceledOnTouchOutside(false);
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void hideCalender() {
		dpBegin.setCalendarViewShown(false);
		dpEnd.setCalendarViewShown(false);
	}

	public DialogChoiceBeginEndTime(Context context, Date begin, Date end) {
		super(context);
		this.mContext = context;
		this.begin = begin;
		this.end = end;
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.btn_ok:
			if (DateUtils.isAfter(begin, end)) {
				ToastHelper.toastShort(mContext, "选择的开始时间晚于结束时间，请重新选择！");
				return;
			}

			if (listener != null) {
				listener.dateChoosen(begin, end);
			}

			break;

		default:
			break;
		}

		this.dismiss();

	}

	@Override
	public void onDateChanged(DatePicker view, int y, int m, int d) {
		canlendar.set(y, m, d);
		Date date = canlendar.getTime();

		switch (view.getId()) {
		case R.id.dp_begin:
			begin = date;
			break;

		case R.id.dp_end:
			end = date;
			break;
		default:
			break;
		}

	}

	public interface DateChoiseListener {
		public void dateChoosen(Date begin, Date end);
	}

	public DateChoiseListener getListener() {
		return listener;
	}

	public void setListener(DateChoiseListener listener) {
		this.listener = listener;
	}
}
